var app,toast;
app = new Vue({
    el: '#app',
    data: {
        form: {
            RoomID: 0
        },
        roomModel: {
            roomList: [],
            currentRoomName: '',
            currentRelationName: '住户名称',
            currentIdList: [],
            IsRoomOpen: false,
            feeList: [],
            IsRoomFeeOpen: false,
            currentFee: {}
        },
        list: [],
        HeJiCost: 0,
        TotalDiscountCost: 0,
        TotalBreakCost: 0,
        TotalChargedCost: 0,
        TotalFeeCount: 0,
        paymentmethods: [],
        allchecked: true,
        calMonth: '',
        totalPreCost: 0,
        idList: [],
        NeedChargeWechatAll: true
    },
    methods: {
        get_data: function () {
            var that = this;
            that.get_fee_data();
        },
        get_fee_data: function () {
            var that = this;
            toast.loading({
                title: "加载中",
                duration: 2000
            }, function (ret) { });

            ns.post({
                action: 'getnewroomfeebyfromroomid',
                FromRoomID: that.form.RoomID,
            }, function (succeed, data, err) {
                toast.hide();
                if (succeed) {
                    that.roomModel.roomList = [];
                    that.list = data.list;
                    data.list.forEach(function (item) {
                        that.roomModel.roomList.push({ id: item.RoomID, name: item.FullName, relationName: item.RealName, isSelected: true })
                    })

                    that.getSelectedData();
                    that.calculateTotalFee();
                } else {
                    api.toast({
                        msg: err,
                        duration: 2000,
                        location: 'bottom'
                    });
                }
            });
        },
        getSelectedData: function () {
            var that = this;
            var nameList = [];
            var relationList = [];
            var idList = [];
            that.roomModel.roomList.forEach(function (item) {
                if (!item.isSelected) {
                    return false;
                }
                if (idList.indexOf(item.id) == -1) {
                    idList.push(item.id)
                }
                if (item.name && nameList.indexOf(item.name) == -1) {
                    nameList.push(item.name)
                }
                if (item.relationName && relationList.indexOf(item.relationName) == -1) {
                    relationList.push(item.relationName)
                }
            })
            that.roomModel.currentIdList = idList;
            that.roomModel.currentRoomName = nameList.length > 0 ? nameList[0] : '';
            that.roomModel.currentRelationName = relationList.length > 0 ? relationList[0] : '';
            var feeList = [];
            that.list.forEach(function (item) {
                if (that.roomModel.currentIdList.indexOf(item.RoomID) == -1) {
                    return false;
                }
                item.list.forEach(function (item2) {
                    item2.Selected = true;
                    // feeList.push(item2);
                })
                feeList.push(item);

            })
            that.roomModel.feeList = feeList;

        },
        doSelectRoom: function (item) {
            var that = this;
            item.isSelected = !item.isSelected;
            that.getSelectedData();
            that.calculateTotalFee();
        },
        doOpenChooseRoom: function () {
            var that = this;
            that.roomModel.IsRoomOpen = true;
        },
        doCloseRoom: function () {
            var that = this;
            that.roomModel.IsRoomOpen = false;
        },
        doViewMoreFee: function () {
            var that = this;
            that.roomModel.IsRoomFeeOpen = !that.roomModel.IsRoomFeeOpen;
        },
        do_reduce_month: function (item) {
            var that = this;
            if (!item.IsCanSelect) {
                return;
            }
            if (!item.NeedChargeWechatAll) {
                item.Selected = true;
            }
            if (item.MonthAddCount > item.MinMonthAddNumber) {
                item.MonthAddCount--;
            }
            item.CalMonthNumber = item.MonthAddNumber * item.MonthAddCount;
            that.get_roomfee_bytime(item);
        },
        do_add_month: function (item) {
            var that = this;
            if (!item.IsCanSelect) {
                return;
            }
            if (!item.NeedChargeWechatAll) {
                item.Selected = true;
            }
            if (item.MaxMonthAddNumber > 0 && item.CalMonthNumber >= item.MaxMonthAddNumber) {
                item.CalMonthNumber = item.MaxMonthAddNumber;
            } else {
                item.MonthAddCount++;
                item.CalMonthNumber = item.MonthAddNumber * item.MonthAddCount;
                if (item.MaxMonthAddNumber > 0 && item.CalMonthNumber >= item.MaxMonthAddNumber) {
                    item.CalMonthNumber = item.MaxMonthAddNumber;
                }
            }
            that.get_roomfee_bytime(item);
        },
        do_select_room: function (item) {
            var that = this;
            item.Selected = !item.Selected;
            for (var i = 0; i < item.list.length; i++) {
                if (!item.list[i].IsCanSelect) {
                    item.list[i].Selected = false;
                    continue;
                }
                item.list[i].Selected = item.Selected;
            }
            that.calculateTotalFee();
        },
        choose_fee: function (item, index) {
            var that = this;
            if (!item.IsCanSelect || item.NeedChargeWechatAll || item.DiscountApproveState == 8) {
                return;
            }
            item.Selected = !item.Selected;
            that.calculateTotalFee();
        },
        calculateTotalFee: function () {
            var that = this;
            total_fee = 0;
            that.TotalDiscountCost = 0;
            that.TotalBreakCost = 0;
            that.TotalChargedCost = 0;
            var idList = [];
            for (var j = 0; j < that.roomModel.feeList.length; j++) {
                for (var i = 0; i < that.roomModel.feeList[j].list.length; i++) {
                    var item = that.roomModel.feeList[j].list[i];
                    //绑定缴费
                    if (item.NeedChargeWechatAll && item.IsCanSelect && item.DiscountApproveState != 8) {
                        total_fee += item.TotalCost;
                        total_fee += item.BreakTotalCost;
                        total_fee -= item.DiscountCost;
                        that.TotalDiscountCost += item.DiscountCost;
                        that.TotalBreakCost += item.BreakTotalCost;
                        that.TotalChargedCost += item.TotalChargedCost;
                        idList.push({
                            ID: item.ID,
                            EndTime: item.EndTime.replace('T', ' '),
                            BreakTotalCost: item.BreakTotalCost,
                            DiscountID: item.DiscountID,
                            DiscountCost: item.DiscountCost
                        });
                    }
                    //单独缴费
                    if (!item.NeedChargeWechatAll && item.IsCanSelect && item.Selected && item.DiscountApproveState != 8) {
                        total_fee += item.TotalCost;
                        total_fee += item.BreakTotalCost;
                        total_fee -= item.DiscountCost;
                        that.TotalDiscountCost += item.DiscountCost;
                        that.TotalBreakCost += item.BreakTotalCost;
                        that.TotalChargedCost += item.TotalPayCost;
                        idList.push({
                            ID: item.ID,
                            EndTime: item.EndTime.replace('T', ' '),
                            BreakTotalCost: item.BreakTotalCost,
                            DiscountID: item.DiscountID,
                            DiscountCost: item.DiscountCost
                        });
                    }
                }
            }
            that.HeJiCost = total_fee;
            that.TotalFeeCount = idList.length;
            that.idList = idList;
        },
        get_roomfee_bytime: function (item) {
            var that = this;
            ns.get({
                action: 'getroomfeebyid',
                id: item.ID,
                endtime: item.EndTime,
                CalMonthNumber: item.CalMonthNumber,
                ChargeID: item.ChargeID,
                StartTime: item.StartTime,
                AutoToMonthEnd: item.AutoToMonthEnd ? 1 : 0
            }, function (succeed, data, err) {
                if (succeed) {
                    item.TotalCost = data.TotalCost;
                    item.BreakTotalCost = data.BreakTotalCost;
                    item.EndTime = data.EndTime;
                    item.DiscountID = data.discountId;
                    item.DiscountCost = data.discountCost;
                    var totalFee = 0;
                    if (data.BreakTotalCost > 0) {
                        totalFee += data.BreakTotalCost;
                    }
                    that.calculateTotalFee();
                }
            });
        },
        select_all: function () {
            var that = this;
            that.allchecked = !that.allchecked;
            for (var j = 0; j < that.roomModel.feeList.length; j++) {
                for (var i = 0; i < that.roomModel.feeList[j].list.length; i++) {
                    var current = that.roomModel.feeList[j].list[i];
                    if (!current.IsCanSelect || current.DiscountApproveState == 8) {
                        current.Selected = false;
                    } else {
                        current.Selected = that.allchecked;
                    }
                }
            }
            that.calculateTotalFee();
        },
        do_open_fee: function (item) {
            item.IsOpen = !item.IsOpen;
        },
        do_pay: function () {
            var that = this;
            that.calculateTotalFee();
            if (that.HeJiCost == 0) {
                api.toast({
                    msg: '收费金额不能为0',
                    duration: 2000,
                    location: 'bottom'
                });
                return
            }
            var idlist = [];
            for (var i = 0; i < that.idList.length; i++) {
                idlist.push(that.idList[i].ID);
            }
            var totalCost = that.HeJiCost + that.TotalDiscountCost - that.TotalBreakCost;
            ns.openWin('shoufeipay_frm', '收费确认', {
                idlist: JSON.stringify(that.idList),
                payCost: that.HeJiCost,
                totalCost: totalCost,
                breakCost: that.TotalBreakCost,
                discountCost: that.TotalDiscountCost,
                count: that.TotalFeeCount,
            });
        },
        do_edit_fee: function (item) {
            var that = this;
            that.roomModel.currentFee = item;
            // var postForm = {
            //     ID: item.ID,
            //     chargeId: item.ChargeID,
            //     feeStartTime: item.StartTime,
            //     feeEndTime: item.EndTime,
            //     chargeName: item.Name,
            //     totalCost: item.TotalCost.toFixed(2),
            //     discountCost: item.DiscountCost.toFixed(2),
            //     breakTotalCost: item.BreakTotalCost.toFixed(2),
            //     useCount: item.UseCount.toFixed(2),
            //     roomId: item.RoomID,
            //     unitPrice: item.UnitPrice,
            //     CalMonthNumber: item.CalMonthNumber
            // }
            ns.openWin('chargeroomfee_edit_frm', '修改账单', {
                postStr: JSON.stringify(that.roomModel.currentFee)
            });
        },
        do_open_add_fee: function () {
            var that = this;
            var roomIdList = [];
            that.roomModel.roomList.forEach(function (item) {
                if (!item.isSelected) {
                    return false;
                }
                if (roomIdList.indexOf(item.id) == -1) {
                    roomIdList.push(item.id)
                }
            })
            if (roomIdList.length == 0) {
                api.toast({
                    msg: '请选择房间',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            ns.openWin('chargeroomfee_add_frm', '临时费用新增', {
                id: that.form.RoomID,
                roomIds: JSON.stringify(roomIdList)
            });
        },
        do_open_fee_history: function () {
            var that = this;
            if (that.roomModel.roomList.length == 0) {
                api.toast({
                    msg: '请选择房间',
                    duration: 2000,
                    location: 'bottom'
                });
                return;
            }
            ns.openWin('analysis_historyfeelist_frm', '已收账单', {
                roomId: that.form.RoomID,
                type: 1
            });
        },
        addTempFee: function (fieldStr) {
            var that = this;
            var data = eval('(' + fieldStr + ')');
            // var index=0;
            for (var j = 0; j < that.roomModel.feeList.length; j++) {
                if (that.roomModel.feeList[j].RoomID == data.roomId) {
                    that.roomModel.feeList[j].list.splice(0, 0, {
                        ID: data.id,
                        RoomID: data.roomId,
                        Name: data.chargeName,
                        TotalCost: Number(data.totalCost),
                        TotalPayCost: 0,
                        BreakTotalCost: 0,
                        DiscountCost: 0,
                        ChargeID: data.chargeId,
                        DiscountID: 0,
                        DiscountApproveState: 0,
                        StartTime: data.startTime,
                        EndTime: data.endTime,
                        UseCount: Number(data.useCount),
                        UnitPrice: Number(data.unitPrice),
                        IsCanSelect: true,
                        NeedChargeWechatAll: false,
                        Selected: true
                    });
                }

            }
            that.calculateTotalFee();
        }
    }
});
apiready = function () {
    api.parseTapmode();
    ns = window.Foresight.Util;
    toast = new auiToast();
    app.form.RoomID = api.pageParam.id;
    app.get_data();
    api.addEventListener({
        name: 'updatewuyejf'
    }, function (ret, err) {
        app.get_data();
    });
    api.addEventListener({
        name: 'saveroomfeefielddone'
    }, function (ret, err) {
        app.roomModel.currentFee.id = ret.value.id;
        app.roomModel.currentFee.DiscountCost = Number(ret.value.DiscountCost);
        app.roomModel.currentFee.DiscountID = ret.value.DiscountID;
        app.roomModel.currentFee.DiscountApproveState = ret.value.DiscountApproveState;
        app.roomModel.currentFee.DiscountApproveStateDesc = ret.value.DiscountApproveStateDesc;

        app.roomModel.currentFee.BreakTotalCost = Number(ret.value.BreakTotalCost);
        app.roomModel.currentFee.TotalCost = Number(ret.value.TotalCost);
        app.roomModel.currentFee.CalMonthNumber = Number(ret.value.CalMonthNumber);
        app.roomModel.currentFee.StartTime = ret.value.StartTime;
        app.roomModel.currentFee.EndTime = ret.value.EndTime;
        app.calculateTotalFee();
    });
    api.addEventListener({
        name: 'do_open_add_fee'
    }, function (ret) {
        app.do_open_add_fee()
    })
    api.addEventListener({
        name: 'do_open_fee_history'
    }, function (ret) {
        app.do_open_fee_history()
    })
    api.addEventListener({
        name: 'saveroomfeetempaddfield'
    }, function (ret, err) {
        app.addTempFee(ret.value.dataField);
    });
}
